<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=Unicode" />
        <title>JD Speed Poller</title>
        <style type="text/css">
			body
			{
				margin: 0;
				width: 130px;
				height: 75px;
				font-family: verdana;
				font-weight: bold;
				font-size: 30px;
			}
			#gadgetContent
			{
				margin-top: 10px;
				vertical-align: middle;
				text-align: center;
				overflow: hidden;
				font-family: Arial;
				font-size: 10px;
				color: #ffffff;
			}
			#test
			{
				margin-top: 30px;
				margin-right: 10px;
				font-size: 20px;
				text-align: right;
			}
			#down
			{	
				margin-left: 5px;
				font-size: 10px;
				text-align: left;
			}
			
			.navi{
				float:left;
				margin-left: 5px;
			}
        </style>
		<script language="javascript" src="jquery.js"></script>
        <script type="text/jscript" language="jscript">
			var refreshRate;
			var port;
			var url;
			
			var counter;
			var theInterval;
		
			$(document).ready(function() {
				counter = 0;
				theInterval = false;
				
				url = "192.168.2.103";
				port = "1337";
				refreshRate = 3;

							

				$.support.cors = true;				
			});
			
			// Initialize the gadget.
			function init()
            {
				
                var oBackground = document.getElementById("imgBackground");
                oBackground.src = "url(background.png)";
				theInterval = setInterval( "caller()", refreshRate * 1000 );
            } 
			
			function caller(){
				var date = new Date();
				
				var theString = "http://"+url+":"+port+"/get/speed?ha4ns=sdd34f"+date.getTime()+"";
				ajax(theString, updateSpeed);
				/*var date = new Date();
				$.get("http://192.168.2.103:1337/get/speed?hans=56546", function(data) {
					$("#test").html("Data Loaded: " + data);
				});
				*/
			}
			
			function startJD(){
				var date = new Date();
				var theString = "http://"+url+":"+port+"/action/start?ha4ns=sdd34f"+date.getTime()+"";
				ajax(theString, updateSpeed);
			}
			
			function pauseJD(){
				var date = new Date();
				var theString = "http://"+url+":"+port+"/action/pause?ha4ns=sdd34f"+date.getTime()+"";
				ajax(theString, updateSpeed);
			}
			
			function stopJD(){
				var date = new Date();
				var theString = "http://"+url+":"+port+"/action/stop?ha4ns=sdd34f"+date.getTime()+"";
				ajax(theString, updateSpeed);
			}
			
			function ajax(url, object){
				if (XMLHttpRequest){
					var request = new XMLHttpRequest();
				}else{
					var request = new ActiveXObject("MSXML2.XMLHTTP.3.0"); 
				}

				
				request.open("POST", url, false);
				request.onreadystatechange = function(){            
					if (request.readyState == 4 && request.status == 200) {            
						if (request.responseText){            
							obj = request.responseText;
							object(obj);
						} else  if(request.responseXML){            
							obj = request.responseXML;
							object(obj);
						}
					} else {
						document.getElementById("test").innerHTML = " error ("+counter+") "+ status;
						counter++;
					}
				}
				request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				request.setRequestHeader("Connection", "close");
				request.send(null);
			}
			
			function updateSpeed($data)
			{
				document.getElementById("test").innerHTML = "" +$data+ " KB/s";
				
			}
	
			
			var settingsUrl;
			var settingsPort;
			var settingsRefresh;
			
			function loadSettings()
				{
						settingsUrl = System.Gadget.Settings.readString("settingsUrl");
						settingsPort = System.Gadget.Settings.readString("settingsPort");
						settingsRefresh = System.Gadget.Settings.read("settingsRefresh");

						if(settingsUrl)
						{
								url = settingsUrl;
						}
						else
						{
								url = "localhost";
						}
						
						if(settingsPort)
						{
								port = settingsPort;
						}
						else
						{
								port = "1337";
						}

						if(refreshRate)
						{
								refreshRate = settingsRefresh;
						}
						else
						{
								refreshRate = 3;
						}
				}

				function settingsClosed(event)
				{
					if(event.closeAction == event.Action.commit)
					{
						$.support.cors = true;
						 if(theInterval)
						{
								clearInterval(theInterval);
						}
						loadSettings();
						theInterval = setInterval( "caller()", refreshRate * 1000 );
					}
					else if (event.closeAction == event.Action.cancel)
					{
					}
				}
	

			System.Gadget.settingsUI = "Settings.html";
			System.Gadget.onSettingsClosed = settingsClosed;

        </script>
    </head>
	
    <body onload="init()">
        <g:background id="imgBackground">
            <div id="gadgetContent">
				<div id="test">
				loading..
				</div>
				<div id="down">
					<div class="navi" id="startButton" onclick="startJD()">Start</div>
					<div class="navi" id="pauseButton" onclick="pauseJD()">Pause</div>
					<div class="navi" id="stopButton" onclick="stopJD()">Stop</div>
				</div>
			</div>
			
			
        </g:background>
		
    </body>
</html>